package com.myself.config;

import com.myself.pojo.Configuration;
import com.myself.pojo.MappedStatement;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.List;

/**
 * 解析映射配置类xml字节输入流数据，并封装到Configuration中
 */
public class XMLMapperBuilder {

    private Configuration configuration;

    public XMLMapperBuilder(Configuration configuration) {
        this.configuration = configuration;
    }

    public void parse(InputStream inputStream) throws DocumentException {

        Document document = new SAXReader().read(inputStream);
        Element rootElement = document.getRootElement();

        String namespase = rootElement.attributeValue("namespace");

        // 1.查询
        List<Element> list = rootElement.selectNodes("//select");
        for (Element element : list) {
            String id = element.attributeValue("id");
            String resultType = element.attributeValue("resultType");
            String paramterType = element.attributeValue("paramterType");
            String sqlText = element.getTextTrim();
            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setResultType(resultType);
            mappedStatement.setParamterType(paramterType);
            mappedStatement.setSql(sqlText);
            String key = namespase + "." + id;
            configuration.getMappedStatementMap().put(key, mappedStatement);
        }

        // 2.添加
        List<Element> insertList = rootElement.selectNodes("//insert");
        for (Element element : insertList) {
            String id = element.attributeValue("id");
            String paramterType = element.attributeValue("parameterType");
            String sqlText = element.getTextTrim();
            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setParamterType(paramterType);
            mappedStatement.setSql(sqlText);
            String key = namespase + "." + id;
            configuration.getMappedStatementMap().put(key, mappedStatement);
        }

        // 3.更新
        List<Element> updatetList = rootElement.selectNodes("//update");
        for (Element element : updatetList) {
            String id = element.attributeValue("id");
            String paramterType = element.attributeValue("parameterType");
            String sqlText = element.getTextTrim();
            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setParamterType(paramterType);
            mappedStatement.setSql(sqlText);
            String key = namespase + "." + id;
            configuration.getMappedStatementMap().put(key, mappedStatement);
        }

        // 4.删除
        List<Element> deleteList = rootElement.selectNodes("//delete");
        for (Element element : deleteList) {
            String id = element.attributeValue("id");
            String paramterType = element.attributeValue("parameterType");
            String sqlText = element.getTextTrim();
            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setParamterType(paramterType);
            mappedStatement.setSql(sqlText);
            String key = namespase + "." + id;
            configuration.getMappedStatementMap().put(key, mappedStatement);
        }

    }
}
